{% extends "cmdb/mbusiness/index.html" %}

{% block right_content %}
<div id="right_menu">
    <b class="modal-title">模块编辑</b>
    <hr/>
    <form class="form-horizontal">
        <input type="text" id="id" value="{{ module_info.id }}" style="display: none;">
        <input type="text" id="moduleNo" value="{{ module_info.module_no }}" style="display: none;">
        <div class="form-inline" style="padding-left:23px">
            <label for="moduleName">模块名</label>
            <input class="form-control" style="width:300px;" type="text" id="moduleName" value="{{ module_info.module_name }}" disabled/>
        </div><br />
        <div class="form-inline" style="padding-left:8px">
            <label>所属分类</label>
            <input class="form-control" style="width:300px;" type="text" value="{{ module_info.parent_category_name }}-{{ module_info.category_name }}" disabled/>
        </div><br />
        <div class="form-inline">
            <label for="project">项目/业务</label>
            <input class="form-control" style="width:300px;" type="text" id="project" value="{{ module_info.project }}" disabled/>
        </div><br />
        <div class="form-inline" style="padding-left:35px">
            <label for="application">应用</label>
            <input class="form-control" style="width:300px;" type="text" id="application" value="{{ module_info.application }}" disabled/>
        </div><br />
        <div class="form-inline" style="padding-left:23px">
            <label for="application">负责人</label>
            <input class="form-control" style="width:300px;" type="text" id="responser"  value="{{ module_info.responser }}"/>
            <span style="display:none"><font color="red">*</font>必填</span>
        </div><br />
        <div class="form-inline" style="padding-left:8px">
            <label for="application">备负责人</label>
            <input class="form-control" style="width:300px;" type="text" id="responserBack"  value="{{ module_info.responser_back }}"/>
            <span style="display:none"><font color="red">*</font>必填</span>
        </div><br />
        <div class="form-inline" style="padding-left:35px">
            <label for="comment">备注</label>
            <input class="form-control" style="width:300px;" type="text" id="comment" value="{{ module_info.comment }}"/>
        </div><br />
        <b class="modal-title">设备配置</b><hr/>
        <table class="table table-bordered" id="equipmentEdit">
            <thead>
                <td class="col">系统ip</td>
                <td class="col">应用目录</td>
                <td class="col">配置文件目录</td>
                <td class="col">server端口</td>
                <td class="col">shutdown端口</td>
                <td class="col">jmx端口</td>
                <td class="col">对外映射端口</td>
                <td class="col">外网端口</td>
                <td class="col">状态</td>
                <td style="background-color:white"><span><img src="{{ STATIC_URL }}img/add.png" onclick="addByScript()"></span></td>
            </thead>
            {% if business_server_mapping %}
            {% for single_server_mapping in business_server_mapping %}
            <tr id="tr_{{ single_server_mapping.number }}">
                <td><input type="text" class="audit" style="background-color:#E6E6FA" id="sys_ip_inner_{{ single_server_mapping.number }}" value="{{ single_server_mapping.sys_ip_inner }}" readonly/></td>
                <td><input type="text" class="audit" style="background-color:#E6E6FA" id="application_path_{{ single_server_mapping.number }}" value="{{ single_server_mapping.application_path }}" readonly/></td>
                <td><input type="text" class="audit" style="background-color:#E6E6FA" id="configure_path_{{ single_server_mapping.number }}" value="{{ single_server_mapping.configure_path }}" readonly/></td>
                <td><input type="text" class="audit" style="background-color:#E6E6FA" id="server_port_{{ single_server_mapping.number }}" value="{{ single_server_mapping.server_port }}" readonly/></td>
                <td><input type="text" class="audit" style="background-color:#E6E6FA" id="shutdown_port_{{ single_server_mapping.number }}" value="{{ single_server_mapping.shutdown_port }}" readonly/></td>
                <td><input type="text" class="audit" style="background-color:#E6E6FA" id="jmx_port_{{ single_server_mapping.number }}" value="{{ single_server_mapping.jmx_port }}" readonly/></td>
                <td><input type="text" class="audit" style="background-color:#E6E6FA" id="outer_mapping_port_{{ single_server_mapping.number }}" value="{{ single_server_mapping.outer_mapping_port }}" readonly/></td>
                <td><input type="text" class="audit" style="background-color:#E6E6FA" id="outline_port_{{ single_server_mapping.number }}" value="{{ single_server_mapping.outline_port }}" readonly/></td>
                <td>
                    <select style="font-size:12px" id="status_{{ single_server_mapping.number }}">
                    {% for idx,single_status in module_status_list.items %}
                        <option value="{{ idx }}" {% ifequal single_server_mapping.use_status idx %} selected {% endifequal %}>{{ single_status }}</option>
                    {% endfor %}
                    </select>
                </td>
                <td><img src="{{ STATIC_URL }}img/delete.png" onclick="del(this)"></td>
            </tr>
            {% endfor %}
            {% endif %}
        </table>
        <input type="text" id="operator" value="{{ request.session.username }}" style="display: none;">
    </form>
    <div style="padding-left: 100px;">
        <button type="button" class="btn btn-primary" id="item_edit">
            确定
        </button>&nbsp;&nbsp;&nbsp;
        <button type="button" class="btn btn-default" id="edit_cancel">
            取消
        </button>
    </div>
</div>

<script type="text/javascript">
    var count = {{ net_count }};
    function addByScript() {
        var equipmentEditId = "tr_" + count;
        var sysInnerIpId = "sys_ip_inner_" + count;
        var applicationPathId = 'application_path_' + count;
        var configurePathId = 'configure_path_' + count;
        var serverPortId = 'server_port_' + count;
        var shutdownPortId = 'shutdown_port_' + count;
        var jmxPortId = 'jmx_port_' + count;
        var outerMappingPortId = 'outer_mapping_port_' + count;
        var outlinePortId = 'outline_port_' + count;
        var responserId = 'responser_' + count;
        var responserBakId = 'responser_back_' + count;
        var statusId = 'status_' + count;
        $("#equipmentEdit").append('<tr id="'+equipmentEditId+'">'
                               + '<td><input type="text" class="audit" id="' + sysInnerIpId + '" /></td>'
                               + '<td><input type="text" class="audit" id="' + applicationPathId + '" /></td>'
                               + '<td><input type="text" class="audit" id="' + configurePathId + '" /></td>'
                               + '<td><input type="text" class="audit" id="' + serverPortId + '" /></td>'
                               + '<td><input type="text" class="audit" id="' + shutdownPortId + '" /></td>'
                               + '<td><input type="text" class="audit" id="' + jmxPortId + '" /></td>'
                               + '<td><input type="text" class="audit" id="' + outerMappingPortId + '" /></td>'
                               + '<td><input type="text" class="audit" id="' + outlinePortId + '" /></td>'
                               + '<td><select style="font-size:12px" id="' + statusId + '">{% for idx,single_status in module_status_list.items %}<option value={{ idx }}>{{ single_status }}</option>{% endfor %}</select></td>'
                               + '<td><img src="{{ STATIC_URL }}img/delete.png" onclick="del(this)"></td>'
                               + '</tr>');
        count++;
    }
    function del(obj) {
        var row = obj.parentNode.parentNode;
        row.parentNode.removeChild(row);
    }

$("#item_edit").click(function(){
    var id = $("#id").val();
    var moduleNo = $("#moduleNo").val();
    var comment = $("#comment").val();
    var operator = $("#operator").val();
    var responser = $("#responser").val();
    var responserBack = $("#responserBack").val();
    var equipmentEditArr = new Array();

    for(var i=1; i < $("#equipmentEdit").children().find('tr').length; i++){
            var trIdStr = $("#equipmentEdit").children().find('tr').eq(i).attr("id");
            var trId =  trIdStr.substr(3);
            var sysIpInner = $("#sys_ip_inner_" + trId).val();
            var applicationPath = $("#application_path_" + trId).val();
            var configurePath = $("#configure_path_" + trId).val();
            var serverPort = $("#server_port_" + trId).val();
            var shutdownPort = $("#shutdown_port_" + trId).val();
            var jmxPort = $("#jmx_port_" + trId).val();
            var outerMappingPort = $("#outer_mapping_port_" + trId).val();
            var outlinePort = $("#outline_port_" + trId).val();
            var use_status = $("#status_" + trId).val();
            var tmpStr ='sys_ip_inner:'+sysIpInner;
            tmpStr += ';application_path:' + applicationPath;
            tmpStr += ';configure_path:' + configurePath;
            tmpStr += ';server_port:' + serverPort;
            tmpStr += ';shutdown_port:' + shutdownPort;
            tmpStr += ';jmx_port:' + jmxPort;
            tmpStr += ';outer_mapping_port:' + outerMappingPort;
            tmpStr += ';outline_port:' + outlinePort;
            tmpStr += ';use_status:' + use_status;
            equipmentEditArr.push(tmpStr);
        }
        var urls = "{% url 'business:meditaction' %}";
        var postdata = {id:id,module_no:moduleNo,responser:responser,responser_back:responserBack,comment:comment,operator:operator, equipment_configure_info:JSON.stringify(equipmentEditArr)}
        var jsonstr = $.ajax({
			url : urls,
			type : "POST",
			data : postdata,
			async : false,
			cache : false
		});
		var jsonobj = eval('(' + jsonstr.responseText + ')');
		if(jsonobj.status == 1){
		    window.location.href="{% url 'business:moduleinfo' %}";
		}else{
		    alert(jsonobj.message);
		}
});

$("#edit_cancel").click(function(){
    window.location.href="{% url 'business:moduleinfo' %}?page=" + {{ page }};
});
</script>
{% endblock %}